<?php

namespace App\Http\Controllers\Comment;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        if(Input::has('title')){
            $name = Input::get('title');
            $id = get_course_id($name);
            $data = DB::table("comments")->whereNull("article_id")->where("course_class_id",$id)->orderBy("id","desc")->paginate(15);
        }else{
            $data = DB::table("comments")->whereNull("article_id")->orderBy("id","desc")->paginate(15);
        }
        //dd($data);
        $this->ret['list']= $data;
        $this->ret['title']= Input::get('title');
        return view("comment.index",$this->ret);
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $id = $request->input("id");
        $re = DB::table("comments")->where("id",$id)->delete();
        if($re){
            //return redirect("comment/index");
            echo "<script>self.location=document.referrer;</script>";
        }else{
            return false;
        }
    }
    public function article_del(Request $request)
    {
        $id = $request->input("id");
        $re = DB::table("article_comments")->where("id",$id)->delete();
        if($re){
            //return redirect("comment/index");
            echo "<script>self.location=document.referrer;</script>";
        }else{
            return false;
        }
    }
    public function article(Request $request){

        if(Input::has('title')){
            $name = Input::get('title');
            $id = get_article_id($name);
            $data = DB::table("article_comments")->where("article_id",$id)->orderBy("id","desc")->paginate(15);
         }else{
            $data = DB::table("article_comments")->orderBy("id","desc")->paginate(15);
         }
        
        $this->ret['list']= $data;
        $this->ret['title']= Input::get('title');
        return view("comment.article",$this->ret);
    }
}
